# -*- coding: utf-8 -*-
from flask import Flask,request
from flask.ext.cors import CORS
import json
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

import BLL.Papers
import BLL.Question
import BLL.Sys
import BLL.Class

app = Flask(__name__)
CORS(app) #解决跨域问题


def FF_InputPostGet(strIn):
    """
    自定义方法：处理前端提交过来的JSON信息
    :param strIn:
    :return:
    """
    try:
        if "data" in strIn:
            _request = eval(strIn.get('data'))
        else:
            for xb in strIn:
                xc = xb
            if xc[0] == '{':
                _request = json.loads(xc)
            else:
                xd = '{' + xc + '}'
                _request = json.loads(xd)
        return _request
    except:
        _request = "JSON解析失败"
        return _request

@app.route('/')
def hello_world():
    return 'Hello World!'


# @app.route('/api/help.json')
# def _api_get_api_help_json():
#     """
#     API帮助文档接口 -- 返回JSON数据
#
#     编写：sdc
#     时间：2016年11月29日 14:40:09
#     :return:
#     """
#     objReturn = BLL.Sys._api_get_api_help_json()
#     _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
#     return _response
#
#
# @app.route('/api/help.html')
# def _api_get_api_help_html():
#     """
#     API帮助文档接口 -- 返回HTNL数据
#
#     编写：sdc
#     时间：2016年11月29日 14:40:58
#     :return:
#     """
#     objReturn = BLL.Sys._api_get_api_help_html()
#     return objReturn


#系统用户 - 登录验证
@app.route('/api/login',methods=["GET","POST"])
def _api_login():
    """
    题库系统的登录接口

    :return:
    """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Sys._api_login(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        return "hello"

@app.route('/api/test.question.add', methods=["GET", "POST"])
def _api_test_question_add():
    '''
    方法：题库--题目新增     负责人：陈公智
    '''
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Question.api_test_question_add(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

@app.route('/api/department.type.view.num', methods=["GET", "POST"])
def _api_department_type_view_num():
    """
        功能：试卷管理 部分-- 根据（部门 + 岗位） 提取题目后按题型分类显示数量  编写：朱盼盼

        :return:
        """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Papers._api_department_type_view_num(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

@app.route('/api/test.question.view.one', methods=["GET", "POST"])
def _api_test_question_view_one():
    '''
    方法：题库--题目查询--单记录     负责人：陈公智
    '''
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Question.api_test_question_view_one(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

@app.route('/api/test.question.del', methods=["GET", "POST"])
def _api_test_question_del():
    '''
    方法：题库--试题--删除    负责人：陈公智
    '''
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Question.api_test_question_del(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

@app.route('/api/test.question.tree.view', methods=["GET", "POST"])
def _api_test_question_tree_view():
    '''
    方法：题库--树形节点查询    负责人：陈公智
    '''
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Question.api_test_question_tree_view(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass


@app.route('/api/test.question.all.view', methods=["GET", "POST"])
def _api_test_question_all_view():
    '''
    方法：题库--全局筛选查询    负责人：陈公智
    '''
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Question.api_test_question_all_view(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 根据条件组卷，生成试卷
@app.route('/api/testpaper.generate.paper', methods=["GET", "POST"])
def _api_testpaper_generate_paper():
    '''
                         方法：根据条件组卷，生成试卷
                         负责人：张耀文
                '''
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Papers._api_testpaper_generate_paper(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 在线答题部分，保存考生答案，客观题顺便计分
@app.route('/api/testpaper.do.submit',methods=["GET","POST"])
def _api_testpaper_do_submit():
    '''
             方法：在线答题部分，保存考生答案，客观题顺便计分
             负责人：张耀文
    '''
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Papers._api_testpaper_do_submit(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

#考官评分信息保存
@app.route('/api/testpaper.check.submit',methods=["GET","POST"])
def _api_testpaper_check_submit():
    '''
                 方法：考官评分信息保存
                 负责人：张耀文
        '''
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Papers._api_testpaper_check_submit(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 试卷查询 - 包含题目、考生答案、参考答案
@app.route('/api/testpaper.view.questionandanswer', methods=["GET", "POST"])
def _api_testpaper_view_questionAndAnswer():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Papers._api_testpaper_view_questionAndAnswer(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        # pass
        return "this is testpaper.view.questionAndAnswer"

# 考官评卷 - 包含题目、考生答案、参考答案
@app.route('/api/testpaper.view.questionandanswer.v2', methods=["GET", "POST"])
def _api_testpaper_view_questionAndAnswer_v2():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Papers._api_testpaper_view_questionAndAnswer_v2(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        # pass
        return "this is testpaper.view.questionAndAnswer"

@app.route('/api/class.add',methods=["GET","POST"])
def _api_class_add():
    """
    功能：分类 -- 新增 编写：sdc

    :return:
    """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Class._api_class_add(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

@app.route('/api/class.edit',methods=["GET","POST"])
def _api_class_edit():
    """
    功能：分类 -- 编辑 编写：sdc

    :return:
    """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Class._api_class_edit(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

@app.route('/api/class.delete',methods=["GET","POST"])
def _api_class_delete():
    """
    功能：分类 -- 逻辑删除 编写：sdc

    :return:
    """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Class._api_class_delete(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

@app.route('/api/class.view.tree',methods=["GET","POST"])
def _api_class_view_tree():
    """
    功能：分类 -- 查询 -- 全部信息（目录树） 编写：sdc

    :return:
    """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Class._api_class_view_tree(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

@app.route('/api/class.view.one',methods=["GET","POST"])
def _api_class_view_one():
    """
    功能：分类 -- 查询 -- 单记录信息查询 编写：sdc

    :return:
    """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Class._api_class_view_one(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass
@app.route('/api/testpaper.view.page.all',methods=["GET","POST"])
def _api_testpaper_view_page_all():
    """
      功能：试卷查询 -- 分页查询 编写：朱盼盼

      :return:
      """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Papers._api_testpaper_view_page_all(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass
@app.route('/api/testpaper.view.title',methods=["GET","POST"])
def _api_testpaper_view_title():
    """
       功能：试卷查询 -- 仅显示题目  编写：朱盼盼

       :return:
       """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Papers._api_testpaper_view_title(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass
@app.route('/api/testpaper.view.title.print',methods=["GET","POST"])
def _api_testpaper_view_title_print():
    """
       功能：试卷查询 -- 显示题目和试卷信息  编写：朱盼盼

       :return:
       """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Papers._api_testpaper_view_title_print(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass
@app.route('/api/testpaper.delete',methods=["GET","POST"])
def _api_testpaper_delete():
    """
       功能：试卷管理 -- 删除  编写：朱盼盼

       :return:
       """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Papers._api_testpaper_delete(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

@app.route('/api/test.question.edit',methods=["GET","POST"])
def _api_test_question_edit():
    """
    接口 题目编辑 编写：宋德聪
    """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Question._api_test_question_edit(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

@app.route('/api/test.question.edit.v2',methods=["GET","POST"])
def _api_test_question_edit_v2():
    """
    接口 题目编辑 编写：宋德聪
    """
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Question._api_test_question_edit_v2(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

if __name__ == '__main__':
    app.run(host='192.168.100.39', port=5004)
    # app.run()
